package com.gm.sfc;

import static com.gm.sfc.util.Utility.*;

import java.util.HashMap;
import java.util.LinkedList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.gm.sfc.adapter.DataAdapter;
import com.gm.sfc.field.IField;
import com.gm.sfc.logic.DataLogic;

/**
 * Data Activity
 * @author $Author: segen.der.gottin@gmail.com $
 * @version $Revision: 19 $
 */
public class DataActivity extends BaseActivity {
    private static final String TAG = "DataActivity";
    private ListView mListView;
    private DataAdapter mDataAdapter;
    private LinkedList<HashMap<IField, Object>> mLinkedList;
    private HashMap<IField, Object> mHashMap;
    private DataLogic mDataLogic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data);
        mDataLogic = DataLogic.getInstance(getApplicationContext());
        mListView = customFindViewById(this, R.id.list_view);

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            String type = (String) bundle.get(EXTRA_TYPE);

            try {
                mLinkedList = mDataLogic.getDataList(type);
                mDataAdapter = new DataAdapter(getApplicationContext(), mLinkedList);
                mListView.setAdapter(mDataAdapter);
                mListView.setOnItemClickListener(new OnItemClickListener() {
                    @SuppressWarnings("unchecked")
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
                        mHashMap = (HashMap<IField, Object>) parent.getAdapter().getItem(position);
                        Intent intent = new Intent(getApplicationContext(), DataDetailActivity.class);
                        intent.putExtra(EXTRA_DATA, mHashMap);
                        startActivity(intent);
                    }
                });
            } catch (Exception e) {
            }
        }
    }
}
